package com.homedo.microservice.open.service.gateway.manager.web.cas;

import com.homedo.common.bean.data.enums.ResponseEnum;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.UserLoginLog;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.enhanced.UserEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.cas.model.UserInfo;
import com.homedo.microservice.open.service.gateway.manager.web.service.UserLoginLogService;
import com.homedo.microservice.open.service.gateway.manager.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Date;

/**
 * 用于加载用户信息 实现UserDetailsService接口，或者实现AuthenticationUserDetailsService接口
 * 若实现UserDetailsService接口的话实现loadUserByUsername方法
 *
 * @author jgshun
 */
public class CustomUserDetailsService implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {
    //    @Autowired
//    private UserRoleService userRoleService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserLoginLogService userLoginLogService;

    @Override
    public UserDetails loadUserDetails(CasAssertionAuthenticationToken token) throws UsernameNotFoundException {
//        System.out.println("当前的用户名是：" + token.getName());

        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(token.getName());

        //1 查用户
        UserEnhanced userEnhanced = userService.getEnhancedByUsername(token.getName(), null);

        if (userEnhanced == null) {
            throw new UsernameNotFoundException(ResponseEnum.USERNAME_NON_EXISTENT.getDesc());
        }

        //2 查角色
        UserContext userContext = new UserContext();
        userContext.setUsername(token.getName());
//        List<UserRoleEnhanced> userRoleEnhanceds = userRoleService.getEnhanceds4User(userContext);

//        Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>();
//        userRoleEnhanceds.forEach(e -> {
//            AuthorityInfo authorityInfo = new AuthorityInfo(e.getRoleEnhanced().getName());
//            authorities.add(authorityInfo);//设置角色
//        });

        //3 查资源
//        List<String> resources = userService.getResourceByUsername(token.getName(), null);
//
//        AuthorityInfo resourcesAuthorityInfo = new AuthorityInfo(RoleConstant.ROLE_EXTRAL_USER_RESOURCE);//设置用户可访问资源
//
//
//        resourcesAuthorityInfo.setResources(resources);
//        authorities.add(resourcesAuthorityInfo);
//
//        userInfo.setAuthorities(authorities);

        UserLoginLog userLoginLog = new UserLoginLog();
        userLoginLog.setCreateTime(new Date());
        userLoginLog.setUpdateTime(new Date());
        userLoginLog.setLoginTime(new Date());
        userLoginLog.setUsername(userEnhanced.getUsername());

        userLoginLogService.saveSelective(userLoginLog, null);

        return userInfo;
    }

}
